<template>
    <div>
        <el-form :model="intentionForm" @submit.native.prevent="onSubmit" :rules="rules" ref="intentionForm" label-width="100px" style="width:460px">
            <el-form-item label="姓名:" prop="cooperationName">
                    <el-input clearable v-model="intentionForm.cooperationName"></el-input>
            </el-form-item>
            <el-form-item label="联系方式:" prop="cooperationMobile">
                    <el-input clearable v-model="intentionForm.cooperationMobile"></el-input>
            </el-form-item>
            <el-form-item label="公司信息" prop="cooperationCompany">
                    <el-input clearable v-model="intentionForm.cooperationCompany"></el-input>
            </el-form-item>
            <el-form-item label="备注信息:" prop="cooperationRemark">
                    <el-input type="textarea" v-model="intentionForm.cooperationRemark"></el-input>
            </el-form-item>
            <el-form-item>
                <button type="submit" class="el-button el-button--primary">{{buttonName}}</button>
                <el-button @click="turnToList">返回列表</el-button>
                <el-button @click="resetForm('intentionForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
export default {
    data() {
        return {
            buttonName: '',
            intentionForm:{},
            res: {
                params: {}
            },
             rules: {
                cooperationName: [
                    { required: true, message: '请输入联系人姓名', trigger: 'blur' },
                    { max: 20, message: '最多20个字符', trigger: 'blur' }
                ],
                cooperationCompany: [
                 { required: true, message: '请输入公司信息', trigger: 'blur' }
                ],
                cooperationMobile: [
                    { required: true, message: '请输入联系方式', trigger: 'change' },
                    { min: 11 , max: 11, message: '必须11位', trigger: 'change' }
                ]
            }
        };
    },
    watch:{
        '$route'(){
            if (!this.$route.params.id){
                this.resetForm('intentionForm')
                this.buttonName = '立即创建'
            }
        }
    },
    methods: {
        getData(){
            this.$axios.get(this.$api.getCooperationList,
                { params: {cooperationId: this.cooperationId}}).then((res) => {
                this.intentionForm = res.data.result[0];
			})
        },
        turnToList(){
            this.$router.push('/intention/intentionList')
        },
        resetForm() {
            this.intentionForm = {};
        },
        onSubmit(event) {
            var requestParam = this.intentionForm
            //新增合作意向
            if (!this.$route.params.id){
                this.$refs['intentionForm'].validate((valid) => {
                  if (valid) {
                    this.$axios(this.$api.insertCooperation,{params:requestParam}).then((res) => {
                        this.$message({
                            message: res.data.msg,
                            type: 'success'
                        });
                        if (res.data.code == 1){
                            this.$router.push('/intention/intentionList')
                        }
                    })
                  }else{
                    console.log('验证不通过');
                  }
                })

            }else{
                //修改合作意向
                this.$refs['intentionForm'].validate((valid) => {
                  if (valid) {
                    requestParam.cooperationId = this.cooperationId;
                    delete requestParam.cooperationCreateTime;
                    this.$axios(this.$api.updateCooperation,{params:requestParam}).then((res) => {
                        this.$message({
                            message: res.data.msg,
                            type: 'success'
                        });
                        if (res.data.code == 1){
                            this.$router.push('/intention/intentionList')
                        }
                    })
                  }else{
                    console.log('验证不通过');
                  }
                })

            }

        }
    },
    activated() {
        this.cooperationId = this.$route.params.id;
        if (this.cooperationId != null ){
            this.getData();
            this.buttonName = '立即更新'
        }else{
            this.resetForm('intentionForm') ;
            this.buttonName = '立即创建'
        }
    },
    deactivated() {

    }
}
</script>
